﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;
using Microsoft.VisualBasic;

namespace CuaHangMayTinh.PresentationLayer
{
    /// <summary>
    /// form model mặt hàng
    /// </summary>
    public partial class FormModelMatHang : Form,IFormTV<ModelMatHangEntity>
    {
        /// <summary>
        /// text lỗi 
        /// </summary>
        private string Text = string.Empty;

        enum TinhTrang
        {
            Đang_bị_khóa,
            Đang_được_bán,
            Đang_hết_hàng
        }
        public DataTable Table { get; set; }

        /// <summary>
        /// danh sách model mặt hàng
        /// </summary>
        private List<ModelMatHangEntity> modelMatHangEntities; 

        /// <summary>
        /// phương thức model mặt hàng
        /// </summary>
        public FormModelMatHang()
        {
            InitializeComponent();
        }

        public FormModelMatHang(bool[] so)
            : this()
        {
            if (so[0])
            {
                toolStripBtn_TimKiem.Enabled = true;
            }
            if (so[1])
            {
                toolStripBtn_Them.Enabled = true;
                themToolStripMenuItem.Enabled = true;
            }
            if (so[2])
            {
                toolStripBtn_Xoa.Enabled = true;
                xoaToolStripMenuItem.Enabled = true;
            }
            if (so[3])
            {
                toolStripBtn_Sua.Enabled = true;
                suaToolStripMenuItem.Enabled = true;
            }
        }

        /// <summary>
        /// load datatbale lên dâtatgidview 
        /// </summary>
        /// <param name="dt"></param>
        public void LoadListView(DataTable dt)
        {
            dataGridViewX_Select.DataSource = dt;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// nút thêm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_them_click(object sender, EventArgs e)
        {
            var modelmathangthem = new FormModelMatHangThem();
            if (modelmathangthem.ShowDialog() == DialogResult.OK)
                LoadListView();
        }

        /// <summary>
        /// nút xóa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_xoa_click(object sender, EventArgs e)
        {
            int i = 0;
            var c = MessageBox.Show("Bạn muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (c == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in dataGridViewX_Select.SelectedRows)
                {
                    if (ModelMatHangManager.Delete((int)row.Cells[0].Value,ref Text))
                    {
                        i++;
                    }
                    else
                    {
                        MessageBox.Show(Text);
                    }
                }
                MessageBox.Show("Xóa thành công " + i + " Model mặt hàng!");
                LoadListView();
            }
        }

        /// <summary>
        /// nút sửa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_sua_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                var row = dataGridViewX_Select.SelectedRows[0];
                var entity = GetEntity(row);
                var modelMatHangThem = new FormModelMatHangThem(entity);
                if (modelMatHangThem.ShowDialog() == DialogResult.OK)
                    LoadListView();
            }
        }

        private ModelMatHangEntity GetEntity(DataGridViewRow row)
        {
            return new ModelMatHangEntity()
            {
                MaModelMH = (int) row.Cells[0].Value,
                MaLoaiMH = LoaiMatHangManager.GetID((string) row.Cells[2].Value),
                Model = (string) row.Cells[1].Value,
                HangSX = (string) row.Cells[3].Value,
                NuocSX = (string) row.Cells[4].Value,
                CauHinh = (string) row.Cells[5].Value,
                BaoHanh = (int) row.Cells[7].Value,
                MaDonViTinh = DonViTinhManager.GetID_Ten((string)row.Cells[8].Value),
                TinhTrang = (int) row.Cells[9].Value,
            };
        }

        /// <summary>
        /// load danh sách model mặt hang lên datagrid
        /// </summary>
        public void LoadListView()
        {
            Table = DataUtil.ChangeColumn(ModelMatHangManager.Find_All(true));
            if (Table != null)
            {
                Table.Columns.Add(new DataColumn("Trạng thái"));
                foreach (DataRow dataRow in Table.Rows)
                {
                    switch ((int) dataRow["Tình Trạng"])
                    {
                        case 0:
                            dataRow["Trạng thái"] = "Đang bị khóa";
                            break;
                        case 1:
                            dataRow["Trạng thái"] = "Đang được bán";
                            break;
                    }
                    if ((int) dataRow["Số lượng"] == 0)
                    {
                        dataRow["Trạng thái"] = "Đang hết hàng";
                        dataRow["Tình trạng"] = 2;
                    }

                }
                dataGridViewX_Select.DataSource = Table;
                dataGridViewX_Select.Columns[0].Visible = false;
                dataGridViewX_Select.Columns["Tình trạng"].Visible = false;
                modelMatHangEntities = ModelMatHangManager.ConvertToList(ModelMatHangManager.Find_All(false));
            }
            
        }

        /// <summary>
        /// sự kiện click nút tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_timkiem_click(object sender, EventArgs e)
        {
            FormModelMatHangTimKiem formModelMatHangTimKiem = new FormModelMatHangTimKiem(Table);
            formModelMatHangTimKiem.ShowDialog();
        }

        /// <summary>
        /// sự kiện thay dổi text của text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStriptxt_timnhanh_TextChanged(object sender, EventArgs e)
        {
            string text = (sender as ToolStripTextBox).Text;
            if (text != "")
            {
                DataTable dt = Table;
                var table2 =dt.AsEnumerable().Where(x =>DataUtil.convertToUnSign3(x.Field<string>("Model").ToLower())
                                    .StartsWith(DataUtil.convertToUnSign3(text.ToLower())));
                LoadListView(table2.Any() ? table2.CopyToDataTable() : null);
            }
            else
            {
                LoadListView(Table);
            }
        }

        /// <summary>
        /// thêm của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void themToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_them_click(sender, e);
        }

        /// <summary>
        /// xóa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_xoa_click(sender, e);
        }

        /// <summary>
        /// sửa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void suaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_sua_click(sender, e);
        }

        private void FormModelMatHang_Load(object sender, EventArgs e)
        {
            modelMatHangEntities = new List<ModelMatHangEntity>();
            LoadListView();
        }

        private void them_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_them_click(sender,e);
        }

        private void sua_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_sua_click(sender,e);
        }

        private void xoa_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_xoa_click(sender,e);
        }

        private void toolStripBtn_In_Click(object sender, EventArgs e)
        {
            if (modelMatHangEntities.Count > 0)
            {
                FormReportList report = new FormReportList(modelMatHangEntities);
                report.Show();
            }
        }


    }
}
